/*
 *
 *  Copyright (C) 2009 GSyC/LibreSoft
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *
 *  Author : Roberto Calvo Palomino <rocapal@gsyc.es>
 *
 */

package com.LibreGeoSocial.App.Overlay;

import java.util.ArrayList;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.location.Location;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

public class TraceOverlay extends Overlay {
	
	private final int mRadius = 5;
	
	private ArrayList<Location> mTraces;
	
	public void setTrace (ArrayList<Location> traces)
	{
		mTraces = traces;
	}
	
	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow)
	{
	
		for (int i=0; i<mTraces.size(); i=i+1)
		{
			
	        GeoPoint geoPoint = new GeoPoint(  (int) (mTraces.get(i).getLatitude() * 1000000), 
					  						   (int) (mTraces.get(i).getLongitude() * 1000000));
	        
			Point point= new Point();
			Projection projection = mapView.getProjection();
		        
			projection.toPixels(geoPoint, point);
		        
			RectF oval = new RectF (point.x - mRadius, point.y - mRadius,
									point.x + mRadius, point.y + mRadius);
		    
			Paint paint = new Paint();
			paint.setARGB(250,255,0,0);
			paint.setAntiAlias(true);
			paint.setFakeBoldText(true);
			
		        
			Paint backPaint = new Paint();
			backPaint.setARGB(175,0,0,0);
			backPaint.setAntiAlias(true);
		        
			String text = Integer.toString(i);
			RectF backRect = new RectF (point.x + 2 + mRadius, point.y - 3*mRadius,
										point.x + 25, point.y + mRadius);
		        
			canvas.drawOval(oval,paint);
			
			if (i==0 || i==mTraces.size()-1 )
			{
				canvas.drawRoundRect(backRect, 5, 5, backPaint);		
				canvas.drawText (text, point.x + 2*mRadius, point.y, paint);
			}
		}
	}

}
